// pages/order/choosepackage/choosepackage.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    falg: false,
		isLastPage: false,
		current: 1,
		size: 100,
  },
	row(e){
		this.onChange(e)
	},
  onChange(event) {
    const index = event.currentTarget.dataset.index;
    let list = this.data.list;
    list[index].checked = !list[index].checked
    this.setData({
      list,
    })
  },
	getData() {
		let {current,size} =this.data
		let parmas = {
			companyId: app.globalData.userInfo.companyData.companyId,
			marketRoleId: 2,//农户
			keyword:this.data.searchValue,
			current,
			size,
		}
		wx.showLoading({
			title: "正在请求数据中"
		})
		wx.$apis.getMarketUser(parmas).then(res => {
			wx.stopPullDownRefresh();
			let old = this.data.list;
			let allList = res.data && res.data.records.map(item => {
				return item
			})
			if (allList && allList.length) {
				let newList = old.concat(allList)
				this.setData({
					list: newList
				})
				if(this.data.farmerList.length){
					// let newList=[...newList]
					this.data.farmerList.map(item1=>{
						newList.map(item2=>{
							if(item1.userId==item2.userId){
								item2.checked=true;
								return
							}
						})
					})
				}
				this.setData({
					list: newList
				})

				if (allList.length < size) {
					this.setData({
						isLastPage: true
					})
				}
			} else {
				this.setData({
					isLastPage: true
				})
			}
		}).catch(err => {
			wx.hideLoading()
		})

	},
  submit() {
    let selectedTypeList = this.data.list.filter(item => item.checked);
    // selectedTypeList.forEach(item=>{
    //   item.mobile=item.mobile
    //   item.realName=item.realName
    // })
		let newList=selectedTypeList.map(item=>{
			return {
				mobile:item.mobile,
				realName:item.realName,
				userId:item.userId
			}
			})
    if (selectedTypeList.length <= 0) {
      wx.showToast({
        title: '请选择农户！',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.setStorage({
        key: "farmerData",
        data: {
					farmerDataList: newList,
        },
        success: function () {
          wx.navigateBack(); //返回上一个页面
        }
      })
    }


  },
  onLoad: function (options) {
		const eventChannel = this.getOpenerEventChannel()
		eventChannel.on('famerListData', (data) => {
			if(data.farmerList){
				this.setData({
					farmerList: data.farmerList,
				})
				this.getData();
				}else{
				this.getData();

			}

	})

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
		this.setData({
			current: 1,
			list: [],
			isLastPage: false
		})
		this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		let {
			current,
			isLastPage
		} = this.data
		if (isLastPage) {
			return
		}
		this.setData({
			current: (Number(current) + 1)
		})
		this.getData()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})